/*
 * Su-yun Information Technology, Co.,Ltd CONFIDENTIAL
 *
 * (C) Copyright Su-yun Information Technology, Co.,Ltd. 2021 All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains the property of
 * Su-yun Information Technology, Co.,Ltd. The intellectual property herein
 * are protected by trade secret, patent law or copyright law. Dissemination
 * of this information or reproduction of this material is strictly forbidden
 * unless prior written permission is obtained from Su-yun Information Technology.
 *
 * https://suyunplat.com/
 *
 */

package com.xtiim.battery.discharge.common;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Configuration util
 *
 */
@Slf4j
public class Configuration {
	private static Map<String, String> scmap = new LinkedHashMap<String, String>();

	/**
	 * Load configuration file
	 *
	 * @param filePath
	 * @throws Exception
	 */
	public static void load(String filePath) throws Exception {
		BufferedReader reader = null;
		try {
			InputStream is = Configuration.class.getClassLoader().getResourceAsStream("application.properties");
			InputStreamReader isr = new InputStreamReader(is);
			reader = new BufferedReader(isr);
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (!line.startsWith("#") && line.indexOf("=") > 0) {
					String[] strs = line.split("=", 2);
					if (strs != null && strs.length == 2) {
						scmap.put(strs[0].trim(), strs[1].trim());
					}
				}
			}
		} catch (Exception e) {

		} finally {
			if (reader != null) {
				reader.close();
			}
		}
	}

	public static String put(String k, String v) {
		return scmap.put(k, v);
	}

	public static String get(String k) {
		return scmap.get(k);
	}

	public static int getInt(String k) {
		String valStr = scmap.get(k);
		if (valStr == null || valStr.equals("")) {
			return -1;
		}

		int val = -1;
		try {
			val = Integer.valueOf(valStr);
		} catch (NumberFormatException e) {
			return -1;
		}

		return val;
	}

	public static String get(String k, String defaultValue) {
		String v = scmap.get(k);
		if (v == null) {
			scmap.put(k, defaultValue);
			return defaultValue;
		} else {
			return v;
		}
	}

}
